import java.io.*;
import java.util.Scanner;

public class test_inputStream {

    public static void main(String[] args) throws IOException {
        try(InputStream is = new FileInputStream("hello.txt")){
            try(Scanner scanner = new Scanner(is, "UTF-8")){
                while(scanner.hasNext()){
                    String s = scanner.next();
                    System.out.print(s);
                }
            }
        }
    }


    public static void main2(String[] args) throws IOException { // read读取单个字符的使用了缓冲区的写法
        // 缓冲区的buf会自动记住 文件指针的位置
        try (InputStream is = new FileInputStream("hello.txt")){
            byte[] buf = new byte[1024];
            int len;
            while(true){
                len = is.read(buf);
                if(len == -1){
                    // 代表文件已经全部读完
                    break;
                }
                // 每次使用 3 字节进行 utf-8 解码，得到中文字符
                for (int i = 0; i < len; i+=3) {          // 如果对象是数字 ， 那么下面三种打印方式都不相同
                    String s = new String(buf, i,3,"UTF-8");
                    System.out.printf("%s",s);
//                    System.out.printf("%c", buf[i]);
//                    System.out.println();
//                    System.out.printf("%s", buf[i]);
//                    System.out.println();
                }
            }

        }
    }

    public static void main1(String[] args) throws IOException {
        File file = new File("hello.txt");
        System.out.println(file.exists());
        System.out.println(file.createNewFile());
        try (InputStream is = new FileInputStream("hello.txt")){
            while(true){
                int b = is.read();
                if(b == -1){
                    // 代表文件已经全部读完
                    break;
                }
                System.out.printf("%c", b);
            }
        }
    }


}
